package com.qifenqian.turbot.module.ui.dish

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.os.Parcelable
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import com.blankj.rxbus.RxBus
import com.qifenqian.turbot.R
import com.qifenqian.turbot.config.ConfigType
import com.qifenqian.turbot.module.adapter.restaurant.DishManageRvAdapter
import com.qifenqian.turbot.module.entity.response.dish.CategoryEntity
import com.qifenqian.turbot.module.entity.response.dish.DishEntity
import com.riqthen.superedittext.SuperEditText
import kotlinx.android.synthetic.main.dialog_edit_text.*
import kotlinx.android.synthetic.main.fragment_tab_dishes.view.*
import org.jetbrains.anko.margin
import org.jetbrains.anko.support.v4.dip
import org.jetbrains.anko.textColor
import java.util.*

/**
 * @author H
 * @createDate 2018/12/21 17:18
 * @description 菜单管理的子fragment
 * @see
 * @since
 */
const val PARAM1 = "dishEntities"
const val PARAM2 = "categoryEntities"
const val PARAM3 = "currentPage"

const val RX_DISH = "rx_dish"
const val RX_POSITION = "rx_position"
const val RX_NEW_PRICE = "new_price"

class TabDishesFragment : Fragment() {

    private var mDishEntities = mutableListOf<DishEntity>() //同一个菜系下的所有菜品
    private var mCategoryEntities = mutableListOf<CategoryEntity>() //所有菜系
    private var mCurrentPage = -1   //当前页数
    private var mAction = ACTION_SPECIAL_DISH_CHOOSE
    private var mPosition = -1

    private var dishManageRvAdapter: DishManageRvAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mAction = (activity!! as DishManageActivity).getAction()
        mPosition = (activity!! as DishManageActivity).getPosition()
        if (arguments != null) {
            mDishEntities = arguments!!.getParcelableArrayList(PARAM1)
            mCategoryEntities = arguments!!.getParcelableArrayList(PARAM2)
            mCurrentPage = arguments!!.getInt(PARAM3)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_tab_dishes, container, false)
        view.rv_tab_dish_manage.layoutManager = GridLayoutManager(context, 3)
        dishManageRvAdapter = DishManageRvAdapter(R.layout.item_dish, mDishEntities)
        val button = Button(activity)
        button.text = "添加菜品"
        button.background = resources.getDrawable(R.drawable.bg_rectangle_line)
        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip(48))
        params.margin = dip(8)
        button.layoutParams = params
        button.textColor = resources.getColor(R.color.black_333333)
        button.setOnClickListener {
            (activity!! as DishManageActivity).setCurrentPage(mCurrentPage)
            DishEditActivity.startThis(activity!!, ConfigType.TYPE_DISH_ADD, mCurrentPage, mCategoryEntities)
        }
        dishManageRvAdapter!!.setOnItemClickListener { adapter, view, position ->
            when (mAction) {
                ACTION_MANAGE -> {
                    (activity!! as DishManageActivity).setCurrentPage(mCurrentPage)
                    DishEditActivity.startThis(activity as Context, ConfigType.TYPE_DISH_EDIT, mCurrentPage, mCategoryEntities, mDishEntities[position])
                }
                ACTION_SPECIAL_DISH_CHOOSE -> {
                    showNewPriceDialog(position)
                }
                ACTION_DISCOUNT_DISH_CHOOSE -> {
                    showNewPriceDialog(position)
                }
            }
        }
        if (mAction == ACTION_MANAGE) {
            dishManageRvAdapter!!.addFooterView(button)
        }
        view.rv_tab_dish_manage.adapter = dishManageRvAdapter
        return view
    }

    private fun showNewPriceDialog(position: Int) {
        val dialog = Dialog(activity!!)
        dialog.setContentView(R.layout.dialog_edit_text)
        dialog.tv_dialog_edit_title.text = "设置优惠价格"
        var etMiddle: SuperEditText? = null
        var etBig: SuperEditText? = null
        if (mDishEntities[position].isSetSize == 0) {
            dialog.et_dialog_edit_content.hint = "请输入优惠价格"
        } else {
            dialog.et_dialog_edit_content.hint = "请输入小份优惠价格"
            etMiddle = SuperEditText(activity!!)
            etMiddle.setBackgroundResource(R.drawable.bg_bottom_line)
            etMiddle.setPadding(dip(8), dip(8), dip(8), dip(8))
            etMiddle.textSize = 14f
            etMiddle.setTextColor(resources.getColor(R.color.black))
            etMiddle.hint = "请输入中份优惠价格"

            etBig = SuperEditText(activity!!)
            etBig.setBackgroundResource(R.drawable.bg_bottom_line)
            etBig.setPadding(dip(8), dip(8), dip(8), dip(8))
            etBig.textSize = 14f
            etBig.setTextColor(resources.getColor(R.color.black))
            etBig.hint = "请输入大份优惠价格"

            dialog.ly_dialog_content.addView(etMiddle)
            dialog.ly_dialog_content.addView(etBig)
        }
        dialog.btn_dialog_edit_ok.setOnClickListener {
            val tempDishEntity: TempDishEntity = if (mDishEntities[position].isSetSize == 0) {  //没有多种价格
                TempDishEntity(mDishEntities[position], newPriceSmall = dialog.et_dialog_edit_content.text.toString().toDouble() * 100, newPriceMiddle = -1.0, newPriceBig = -1.0, position = mPosition)
            } else {
                TempDishEntity(mDishEntities[position], newPriceSmall = dialog.et_dialog_edit_content.text.toString().toDouble() * 100, newPriceMiddle = etMiddle!!.text.toString().trim().toDouble(), newPriceBig = etBig!!.text.toString().trim().toDouble(), position = mPosition)
            }
            RxBus.getDefault().post(tempDishEntity, RX_DISH)
            dialog.dismiss()
            activity!!.finish()
        }
        dialog.btn_dialog_edit_cancel.setOnClickListener { dialog.dismiss() }
        dialog.show()
    }

    fun updateArguments(dishEntities: MutableList<DishEntity>, categoryEntities: MutableList<CategoryEntity>, currentPage: Int) {
        mDishEntities = dishEntities
        mCategoryEntities = categoryEntities
        mCurrentPage = currentPage
        val bundle = arguments
        bundle?.putParcelableArrayList(PARAM1, mDishEntities as ArrayList<out Parcelable>)
        bundle?.putParcelableArrayList(PARAM2, mCategoryEntities as ArrayList<out Parcelable>)
        bundle?.putInt(PARAM3, currentPage)
    }
}

data class TempDishEntity(
        var dishEntity: DishEntity,
        var newPriceSmall: Double,
        var newPriceMiddle: Double,
        var newPriceBig: Double,
        var position: Int)
